package com.jinlejia.erp.ui.mine.contract;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.google.android.material.tabs.TabLayout;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.ViewPagerAdapter;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.bean.PactAttachBean;
import com.jinlejia.erp.databinding.ActivityCheckContractBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 查看合同
 */
public class CheckContractActivity extends AppCompatActivity {

    // 填充到ViewPager中的Fragment
    private List<Fragment> mFragments = new ArrayList<>();
    private ViewPagerAdapter viewPagerAdapter;
    private TextFragment textFragment;
    private AttachFragment attachFragment;

    ContractSelect contractSelect;
    ArrayList<PactAttachBean> list;
    private PactAttachBean checkedBean;

    private ActivityCheckContractBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_check_contract);
        binding.setEvent(this);
        binding.idTableLayout.addTab(binding.idTableLayout.newTab().setText("文本"));
        binding.idTableLayout.addTab(binding.idTableLayout.newTab().setText("附件"));
        textFragment = new TextFragment();
        attachFragment = new AttachFragment();

        mFragments.add(0, textFragment);       //文本
        mFragments.add(1, attachFragment);    //附件

        viewPagerAdapter = new ViewPagerAdapter(this.getSupportFragmentManager(), new String[0], mFragments);
        binding.viewPager.setAdapter(viewPagerAdapter);
        binding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.idTableLayout));
        binding.idTableLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                binding.viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        getData();

    }

    private void setViewPager(boolean text, boolean attach) {
        mFragments.clear();
        binding.idTableLayout.removeAllTabs();
        binding.viewPager.removeAllViewsInLayout();
        if (text) {
            binding.idTableLayout.addTab(binding.idTableLayout.newTab().setText("文本"));
            mFragments.add(0, textFragment);
            binding.textAttach.setVisibility(View.VISIBLE);
        } else {
            binding.textAttach.setVisibility(View.GONE);
        }
        if (attach) {
            if (text) {
                binding.idTableLayout.addTab(binding.idTableLayout.newTab().setText("附件"));
                mFragments.add(1, attachFragment);
            } else {
                binding.idTableLayout.addTab(binding.idTableLayout.newTab().setText("附件"));
                mFragments.add(0, attachFragment);
            }
            binding.fileAttach.setVisibility(View.VISIBLE);
        } else {
            binding.fileAttach.setVisibility(View.GONE);
        }
        viewPagerAdapter.notifyDataSetChanged();


    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.current_contract_layout:
                contractSelect = new ContractSelect(CheckContractActivity.this, list, checkedBean);
                contractSelect.setOnSelectListener(o -> {
                    setData(o);
                    checkedBean = o;
                });
                contractSelect.show();
                break;
            default:
                break;
        }
    }

    private void setData(PactAttachBean o) {
        switch (o.getType()) {
            case 0:
                //文本
                if (TextUtils.isEmpty(o.getHtmlUrl())) {
                    binding.contentLayout.setVisibility(View.GONE);
                } else {
                    binding.contentLayout.setVisibility(View.VISIBLE);
                    textFragment.setUrl(o.getHtmlUrl());
                }
                setViewPager(true, false);
                break;
            case 1:
                //附件
                if (o.getAttachFiles().size() == 0) {
                    binding.contentLayout.setVisibility(View.GONE);
                } else {
                    binding.contentLayout.setVisibility(View.VISIBLE);
                    attachFragment.setData(o.getAttachFiles());
                }
                setViewPager(false, true);
                break;
            case 2:
                if (TextUtils.isEmpty(o.getHtmlUrl()) && o.getAttachFiles().size() == 0) {
                    binding.contentLayout.setVisibility(View.GONE);
                } else {
                    binding.contentLayout.setVisibility(View.VISIBLE);
                    textFragment.setUrl(o.getHtmlUrl());
                    attachFragment.setData(o.getAttachFiles());
                }
                setViewPager(true, true);
                break;
            default:
                break;
        }
        //当前合同
        binding.currentContract.setText(o.getPactOrAttachName());
    }

    private void getData() {
        OkHttpUtils
                .get()
                .url(ApiConfig.CONTRACT_ATTACH_LIST)
                .addParams("contractCode", getIntent().getStringExtra("contractCode"))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(CheckContractActivity.this, response)) {
                            JSONArray data = JSON.parseArray(response.get("data").toString());
                            binding.contentLayout.setVisibility(View.VISIBLE);
                            //数据列表
                            list = JSON.parseObject(data.toString(), new TypeReference<ArrayList<PactAttachBean>>() {
                            });
                            if (list.size() > 0) {
                                PactAttachBean bean = list.get(0);
                                checkedBean = bean;
                                setData(bean);
                            }
                        } else if (response.get("code").toString().equals("501")) {
                            binding.contentLayout.setVisibility(View.GONE);
                        }
                    }

                });
    }
}
